#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
struct student //single link
{
    int num;
    float score;
    struct student * next;
};

typedef int NUM;
typedef struct connect
{
    int num;
    float score;
    struct student * next;
} CON;

typedef struct student_os{
	int num;
	float score;
	struct student *next;
}stu;

int n=0;//node count, if n=1, just one node and it is head node.
stu *slink_create()
{
	printf("create init sigle link\n");
	stu *head,*p1,*p2;
	head=NULL;
	p1=(stu *)malloc(sizeof(stu));
	p2=p1;
	printf("please input num(>0) and score(num score):\n");
	scanf("%d %f",&p1->num,&p1->score);
	while(p1->num !=0){
		n=n+1;
		if(n==1)
			head=p1; //just only one node, and this is head node
		else
			p2->next=p1;
		p2=p1;
		p1=(stu *)malloc(sizeof(stu));
		scanf("%d %f",&p1->num,&p1->score);
	}
	p2->next=NULL;
	free(p1);
	p1=NULL;
	return head;
}

void msg_show(stu *head)
{
	printf("show message:\n");
	while(head!=NULL){
		printf("%-4d %5.1f\n",head->num,head->score);
		head=head->next;
	}
}

stu *delnode(stu *head,int key)
{
	printf("will delete node num=%d\n",key);
	stu *p1;//save need check node
	stu *p2;//save checked node
	if(head==NULL) 
		return NULL; //cannot delet null link
	p1=head;
	while(p1->num !=key && p1->next !=NULL){
		p2=p1; //save corrent node address
		p1=p1->next;//move to next node
	}
	if(p1->num==key) //find it
	{
		if(p1==head) //if delete node is the first node
			head=p1->next;//head change to p1 next, so the head node is deletetd
		else
			p2->next=p1->next;
		free(p1);
		p1=NULL;
		printf("delete %d success\n",key);
		n=n-1;//node count reduce
	}else
		printf("note find %d node\n",key);
	return head;
}

stu *insertnode(stu *head, int key, stu *knode)
{
	stu *p1;// save corrent need check node address
	if(head==NULL)
	{
		head=knode;
		knode->next=NULL;
		n=n+1;// node count +1
		return head;
	}
	p1=head;
	while(p1->num !=key && p1->next !=NULL)
		p1=p1->next;//move to next node
	if(key==p1->num) //find it
	{
		knode->next=p1->next;//knode next node is orginal p1's next
		p1->next=knode;// knode insert to p1 behind
		n=n+1;
	}else
		printf("not find %d\n",key);
	return head;
}

void print_stu(struct student * head)
{
    struct student * p;
    printf("record is %d\n", n);
	/*
	   for(p = head->next; p; p = p->next)
	   {
	   printf("%-4d %5.1f\n", p->num, p->score);
	   }
	   */
	  p = head;
    if(head != NULL)
        do
        {
            printf("%-4d %5.1f\n", p->num, p->score);
            p = p->next;
        }
        while(p != NULL);
}

int static_list()
{
    struct student a, b, c, *head, *p;
    a.num = 1;
    a.score = 99.6;
    b.num = 2;
    b.score = 89.5;
    c.num = 3;
    c.score = 87.5;
    head = &a;
    a.next = &b;
    b.next = &c;
    c.next = NULL;
    print_stu(head);
    return 0;
}

struct student * malloc_create()
{
    printf("sizeof(float)=%d\n", (int)sizeof(float));
    struct student * head;
    struct student * p1, *p2;
    printf("sizeof(struct student)=%d\n", (int)sizeof(struct student));
    p1 = (struct student *)malloc(sizeof(struct student));
    p2 = p1;
    printf("please input num and score,note:num is not 0\n");
    scanf("%d %f", &p1->num, &p1->score);
    head = NULL;
    n = 0;
    while(p1->num != 0)
    {
        n = n + 1;
        if(n == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct student *)malloc(sizeof(struct student));
        scanf("%d %f", &p1->num, &p1->score);
    }
    p2->next = NULL;
    return (head);

}

/*-------------------end of single link table------------*/

int main (int agrc, char ** argv)
{
    CON con;
    con.num = 9;
    printf("connet num=%d\n", con.num);
    NUM a = 1;
    printf("NUM a=%d\n", a);
    struct student * pt_m;
    static_list();
//    pt_m = malloc_create();
  //  print_stu(pt_m);
	stu *head=NULL;
	head=slink_create();
	msg_show(head);
	//memset(&head,0,sizeof(stu *head));
	int del_num=0;
	printf("input delete node(num end of 0):\n");
	scanf("%d\n",&del_num);
	head=delnode(head,del_num);
	msg_show(head);
	stu *inode;
	inode=(stu *)malloc(sizeof(stu));
	int inum=0;
	printf("please input insert node(num score):\n");
	scanf("%d %f",&inode->num,&inode->score);
	printf("please input insert behind num(end of 0):\n");
	scanf("%d",&inum);
	head=insertnode(head,inum,inode);
	msg_show(head);
	free(inode);
	inode=NULL;
    return 0;
}
